/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.cs.db.DbResults;
/*     */ import com.zimbra.cs.db.DbScheduledTask;
/*     */ import com.zimbra.cs.db.DbUtil;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.ScheduledTask;
/*     */ import com.zimbra.cs.mailbox.ScheduledTaskManager;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestScheduledTaskManager
/*     */   extends TestCase
/*     */ {
/*     */   static final String TASK_NAME = "TestTask";
/*     */   private static final String USER_NAME = "user1";
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  40 */     cleanUp();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testSingleTask()
/*     */     throws Exception
/*     */   {
/*  49 */     checkNumPersistedTasks(0);
/*     */     
/*     */ 
/*  52 */     TestTask task = new TestTask();
/*  53 */     long now = System.currentTimeMillis();
/*  54 */     task.setExecTime(new Date(now + 1000L));
/*  55 */     Mailbox mbox = TestUtil.getMailbox("user1");
/*  56 */     task.setMailboxId(mbox.getId());
/*  57 */     ScheduledTaskManager.schedule(task);
/*     */     
/*     */ 
/*  60 */     checkNumPersistedTasks(1);
/*  61 */     Thread.sleep(1250L);
/*     */     
/*  63 */     assertEquals("TestTask was not called", 1, task.getNumCalls());
/*  64 */     checkNumPersistedTasks(0);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testRecurringTask()
/*     */     throws Exception
/*     */   {
/*  74 */     checkNumPersistedTasks(0);
/*     */     
/*     */ 
/*  77 */     TestTask task = new TestTask();
/*  78 */     task.setIntervalMillis(200L);
/*  79 */     Mailbox mbox = TestUtil.getMailbox("user1");
/*  80 */     task.setMailboxId(mbox.getId());
/*  81 */     ScheduledTaskManager.schedule(task);
/*     */     
/*     */ 
/*  84 */     checkNumPersistedTasks(1);
/*  85 */     Thread.sleep(1000L);
/*     */     
/*     */ 
/*  88 */     ScheduledTaskManager.cancel(TestTask.class.getName(), "TestTask", mbox.getId(), false);
/*  89 */     Thread.sleep(200L);
/*  90 */     int numCalls = task.getNumCalls();
/*  91 */     assertTrue("Unexpected number of task runs: " + numCalls, numCalls > 0);
/*  92 */     checkNumPersistedTasks(0);
/*     */     
/*     */ 
/*  95 */     Thread.sleep(400L);
/*  96 */     assertEquals("Task still ran after being cancelled", numCalls, task.getNumCalls());
/*     */   }
/*     */   
/*     */   public void testTaskProperties() throws Exception
/*     */   {
/* 101 */     checkNumPersistedTasks(0);
/*     */     
/*     */ 
/* 104 */     ScheduledTask task = new TestTask();
/* 105 */     long now = System.currentTimeMillis();
/* 106 */     task.setExecTime(new Date(now + 60000L));
/* 107 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 108 */     task.setMailboxId(mbox.getId());
/* 109 */     task.setProperty("prop1", "value1");
/* 110 */     task.setProperty("prop2", "value2");
/* 111 */     task.setProperty("prop3", null);
/* 112 */     ScheduledTaskManager.schedule(task);
/*     */     
/*     */ 
/* 115 */     checkNumPersistedTasks(1);
/*     */     
/*     */ 
/* 118 */     List<ScheduledTask> tasks = DbScheduledTask.getTasks(TestTask.class.getName(), mbox.getId());
/* 119 */     assertEquals(1, tasks.size());
/* 120 */     task = (ScheduledTask)tasks.get(0);
/* 121 */     assertEquals("value1", task.getProperty("prop1"));
/* 122 */     assertEquals("value2", task.getProperty("prop2"));
/* 123 */     assertEquals(null, task.getProperty("prop3"));
/*     */     
/*     */ 
/* 126 */     ScheduledTaskManager.cancel(TestTask.class.getName(), task.getName(), mbox.getId(), true);
/* 127 */     checkNumPersistedTasks(0);
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 132 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void cleanUp() throws Exception
/*     */   {
/* 137 */     Mailbox mbox = TestUtil.getMailbox("user1");
/* 138 */     ScheduledTaskManager.cancel(TestTask.class.getName(), "TestTask", mbox.getId(), true);
/*     */   }
/*     */   
/*     */   private void checkNumPersistedTasks(int expected) throws Exception
/*     */   {
/* 143 */     DbResults results = DbUtil.executeQuery("SELECT COUNT(*) FROM " + DbScheduledTask.TABLE_SCHEDULED_TASK + " WHERE class_name = '" + TestTask.class.getName() + "'", new Object[0]);
/*     */     
/*     */ 
/* 146 */     assertEquals("Unexpected number of persisted tasks", expected, results.getInt(1));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestScheduledTaskManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */